package com.soul.doc.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.soul.common.msg.ApiResult;
import com.soul.doc.bean.dto.CollectionDto;
import com.soul.doc.bean.dto.MusicSearchDto;
import com.soul.doc.bean.dto.MusicAddDto;
import com.soul.doc.bean.dto.MusicUpdateDto;
import com.soul.doc.bean.vo.MusicVo;
import com.soul.doc.service.MusicService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.soul.common.controller.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sws
 * @since 2023-11-19 11:08:12
 */
@RestController
@RequestMapping("/music")
public class MusicController extends BaseController {

    @Autowired
    private MusicService musicService;


    @GetMapping("/{page}/{size}/list/ext")
    public ApiResult<IPage<MusicVo>> queryUserList(@PathVariable("page") Integer page, @PathVariable("size") Integer size, MusicSearchDto searchDto){
        IPage<MusicVo> userVoIPage = this.musicService.queryMusicList(page, size, searchDto);
        return ApiResult.success(userVoIPage);
    }

    /**
     * 新增歌曲
     * @param addDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增歌曲")
    public ApiResult<?> add(@RequestBody MusicAddDto addDto){
        this.musicService.addMusic(addDto);
        return ApiResult.success();
    }

    @PostMapping("/add/love")
    public ApiResult<MusicVo> addLove(@RequestBody CollectionDto collectionDto){
        boolean b = this.musicService.addLove(collectionDto);
        if (b){
            MusicVo musicVo = musicService.getMusicInfoById(collectionDto.getMusicId());
            return ApiResult.success(musicVo);
        }
        return ApiResult.success();
    }

    @PostMapping("/add/history")
    public ApiResult<?> addHistory(@RequestBody CollectionDto collectionDto){
        this.musicService.addHistory(collectionDto);
        return ApiResult.success();
    }
    @PostMapping("/add/{type}/num/{musicId}/ext")
    public ApiResult addMusicPlayNum(@PathVariable("musicId") Long musicId,@PathVariable("type") String type){
        if (ObjectUtil.equals(type,"play")){
            musicService.addPlayNum(musicId);
        }else if (ObjectUtil.equals(type,"collect")){
            musicService.addCollectNum(musicId,"add");
        }
        return ApiResult.success();
    }

    @GetMapping("/{id}/info/ext")
    public ApiResult<MusicVo> getMusicInfo(@PathVariable Long id){
        MusicVo musicVo = musicService.getMusicInfoById(id);
        return ApiResult.success(musicVo);
    }

    @GetMapping("/my/history")
    public ApiResult<List<MusicVo>> getMusicHistory(Integer type){
        // 获取最近听歌记录
        List<MusicVo> musicHistory = musicService.getMusicHistory(type);
        return ApiResult.success(musicHistory);
    }

    @PutMapping("/update")
    public ApiResult<?> updateMusic(@RequestBody MusicUpdateDto updateDto){
        musicService.updateMusic(updateDto);
        return ApiResult.success();
    }

}
